package risk.control.application.activity;

import android.os.Bundle;
import android.os.CountDownTimer;

import java.util.HashMap;

import risk.control.application.R;
import risk.control.application.activity.base.BaseActivity;
import risk.control.application.http.Api;
import risk.control.application.http.callback.UiCallback;
import risk.control.application.utils.RouteJump;
import risk.control.application.utils.TitleBarUtils;
import risk.control.application.utils.data.TokenUtils;
import risk.control.application.utils.mmkv.MMKVUtils;
import risk.control.application.utils.mmkv.MMKV_KEY;

/**
 * 启动页
 */
public class StartUpActivity extends BaseActivity {

    //倒计时时长
    private static final int DURATION = 2;
    private CountDownTimer mDownTime;

    @Override
    protected void onResume() {
        TitleBarUtils.setFullScreen_v2(this);
        super.onResume();
    }

    @Override
    protected void postData(HashMap<String, String> dataMap) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        initView();
        initData();
        initListener();
    }

    private void initView() {

    }


    private void initData() {

    }

    private void initListener() {
        mDownTime = new CountDownTimer(DURATION * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //每隔countDownInterval秒会回调一次onTick()方法
            }

            @Override
            public void onFinish() {
                //倒计时完成
                goUserInfo();
            }
        }.start();
    }

    private void goUserInfo() {
        String token = TokenUtils.get();
        if (token.isEmpty()) {
            //跳转登录
            RouteJump.start(StartUpActivity.this, RouteJump.LOGIN_ACTION, null, true, false);
            return;
        }
        //token登录
        HashMap<String, String> params = new HashMap<>();
        params.put("token", token);//token参数
        params.put("loginType", "1");
        getNetWordData(Api.login, params, new UiCallback() {
            @Override
            public void onSuccess(String respData) {
                //持久化用户信息
                MMKVUtils.putString(MMKV_KEY.KV_USER_INFO_DATA, respData);
                //信息录用页
                RouteJump.start(StartUpActivity.this, RouteJump.MAIN_ACTION, null, true, false);
            }

            @Override
            public void onSpecialCode(String errorData) {

            }

            @Override
            public void onResolve() {

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDownTime != null) {
            mDownTime.cancel();
        }
    }
}

